package com.uniledger.exchange.login.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.hbb20.CountryCodePicker;
import com.uniledger.exchange.R;
import com.uniledger.exchange.login.Admin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 电话号码
 */
public class SignUpTwoFragment extends SignUpFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private Admin admin;
    private String mParam2;

    @BindView(R.id.ccp)
    CountryCodePicker ccp;

    @BindView(R.id.editText_carrierNumber)
    EditText editTextCarrierNumber;


    public SignUpTwoFragment() {
        // Required empty public constructor
    }

    public static SignUpTwoFragment newInstance(Admin param1, String param2) {
        SignUpTwoFragment fragment = new SignUpTwoFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            admin = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sign_up_two, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ccp.registerCarrierNumberEditText(editTextCarrierNumber);
    }

    @Override
    public void onClick(View v) {
        String tel = editTextCarrierNumber.getText().toString().trim();
        if (TextUtils.isEmpty(tel)) {

        } else {
            admin.mobile = replaceBlank(tel);
            admin.countryCode = ccp.getSelectedCountryCode();
            upListener.getMobile(admin);
        }

    }

    protected String replaceBlank(String str){
        String dest = null;
        if(str == null){
            return dest;
        }else{
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
            return dest;
        }
    }
}
